#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , m_pQHBoxLayout(nullptr)
    , m_pQTabWidget(nullptr)
    , m_uiFormKeyIndex(0)
{
    ui->setupUi(this);

    // 初始化配置参数
    AnaConfig::Instance().init();
    gMsgShow::Instance().init();
    gSetDialog::Instance().init();
    gLogShower::Instance().init();
    gChatServer::Instance().init();

    m_mapFormSet.clear();

    // 设置窗口图标和名称
    setWindowIcon(QIcon(QString(":/operationIcons/resource/operationIcons/main_icon.ico")));
    setWindowTitle(QStringLiteral("我的工具集"));
    // 设置窗口默认大小
    resize(1000, 700);

    m_pQHBoxLayout = new QHBoxLayout();
    ui->centralwidget->setLayout(m_pQHBoxLayout);
    m_pQHBoxLayout->setSpacing(0);
    m_pQHBoxLayout->setContentsMargins(0, 0, 0, 0);
    m_pQTabWidget = new QTabWidget(this);
    m_pQTabWidget->setStyleSheet(gStyle_TabWidgetSet);
    //m_pQTabWidget->setFont(gstFont);
    m_pQHBoxLayout->addWidget(m_pQTabWidget);

    // 设置主页
    FormHomePage *pFormHome = new FormHomePage(this);
    m_pQTabWidget->addTab(pFormHome, "HomePage");
    connect(pFormHome, SIGNAL(sendNewForm(uint)), this, SLOT(slt_newTab(uint)));
    connect(pFormHome, SIGNAL(sendCloseAll()), this, SLOT(slt_closeAll()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

// 主窗口关闭确认
void MainWindow::closeEvent(QCloseEvent * event)
{
    if(true == AnaConfig::Instance().getCloseCheck())
    {
        switch( QMessageBox::information(this, QString("退出"), QString("  确认退出应用程序?   "), QString("是"), QString("否"), 0, 1 ) )
        {
            case 0:
                event->accept();
                // 主进程退出前，需释放聊天服务端，否则出现崩溃
                gChatServer::Instance().close();
                gChatServer::Instance().release();
                break;
            case 1:
            default:
                event->ignore();
                break;
        }
    }
    else
    {
        gChatServer::Instance().close();
        gChatServer::Instance().release();
    }
}

// 移除指定的标签页
void MainWindow::slt_removeTab(unsigned int iKey)
{
    auto it = m_mapFormSet.find(iKey);
    if(m_mapFormSet.end() != it)
    {
        m_pQTabWidget->removeTab(it->first);

        delete it->second;
        m_mapFormSet.erase(it);
    }
}

// 获取标签页状态信息并展示
void MainWindow::slt_statusMsg(QString& strMsg)
{
    statusBar()->showMessage(strMsg);
}

// 新建标签页
void MainWindow::slt_newTab(uint iTabType)
{
    BaseFormWidget *pNewForm = nullptr;
    QString strTabTitle;
    switch (iTabType) {
        case FORM_BASE64:
            pNewForm = static_cast<BaseFormWidget* >(new FormBase64Proc(this));
            strTabTitle = "Base64";
            break;
        case FORM_JSONTREE:
            pNewForm = static_cast<BaseFormWidget* >(new FormJSONView(this));
            strTabTitle = "JSONView";
            break;
        case FORM_CHATROOM:
            pNewForm = static_cast<BaseFormWidget* >(new FormChatRoom(this));
            strTabTitle = "鱼塘";
            break;
        case FORM_PICRECT:
            pNewForm = static_cast<BaseFormWidget* >(new FormPicRect(this));
            strTabTitle = "PicRect";
            break;
        default:
            myMessageBoxShowOne(FORM_NULL, MSG_SHOW_WARN, QString("功能开发中，暂不支持，看看其他的吧，家人们"));
            break;
    }

    if(nullptr == pNewForm)
    {
        return;
    }

    m_pQTabWidget->addTab(pNewForm, strTabTitle);
    m_pQTabWidget->setCurrentWidget(pNewForm);
    pNewForm->init();
    connect(pNewForm, SIGNAL(SendClose(uint)), this, SLOT(slt_removeTab(uint)));
    connect(pNewForm, SIGNAL(SendStatusMsg(QString&)), this, SLOT(slt_statusMsg(QString&)));

    uint Curidx = m_pQTabWidget->currentIndex();
    m_mapFormSet[Curidx] = pNewForm;
    pNewForm->setFormIndex(Curidx, (FORM_PROC_TYPE)iTabType);
}

// 关闭所有标签页
void MainWindow::slt_closeAll()
{
    uint iFormSize = m_mapFormSet.size();
    for(auto it : m_mapFormSet)
    {
        m_pQTabWidget->removeTab(it.first);
        delete it.second;
    }

    m_mapFormSet.clear();

    myMessageBoxShowOne(FORM_NULL, MSG_SHOW_INFO, QString("  共关闭 %1 个标签页   ").arg(QString::number(iFormSize)));
}
